package com.tuanzi.loan.vo;

import org.apache.commons.lang3.BooleanUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import lombok.Data;

/**
 * @author Winson
 * @date 2019-01-28
 */

@Data
public class ProductRule {

	private FilteringRule filteringRule = new FilteringRule();

	private ValueRule valueRule = new ValueRule();

	private SubstitutionRule substitutionRule = new SubstitutionRule();

	private SortRule sortRule = new SortRule();

	private FillRule fillRule = new FillRule();


	public JSONObject genJsonObjectByType(String type) {
		SearchParameter parameter = new SearchParameter();

		if ("all".equals(type) || "averagePrice".equals(type)) {
			parameter.setKey(type);
		}

		if ("latestCreateTime".equals(type)) {
			parameter.setKey(type);
			SearchParameter.LatestCreateTimeSearchParameter latestCreateTimeSearchParameter = new SearchParameter.LatestCreateTimeSearchParameter();

			if (0 == this.getValueRule().getCreateTimeType()) {
				latestCreateTimeSearchParameter.setCreateTime(7);
			}

			if (1 == this.getValueRule().getCreateTimeType()) {
				latestCreateTimeSearchParameter.setCreateTime(15);
			}

			if (2 == this.getValueRule().getCreateTimeType()) {
				latestCreateTimeSearchParameter.setCreateTime(30);
			}

			parameter.setLatestCreateTimeSearchParameter(latestCreateTimeSearchParameter);
		}

		if ("quotaInterval".equals(type)) {
			parameter.setKey(type);
			SearchParameter.QuotaIntervalSearchParameter quotaIntervalSearchParameter = new SearchParameter.QuotaIntervalSearchParameter();

			if (0 == this.getValueRule().getQuotaIntervalType()) {
				quotaIntervalSearchParameter.setMin(3000);
			}

			if (1 == this.getValueRule().getQuotaIntervalType()) {
				quotaIntervalSearchParameter.setMin(3000);
				quotaIntervalSearchParameter.setMax(5000);
			}

			if (2 == this.getValueRule().getQuotaIntervalType()) {
				quotaIntervalSearchParameter.setMin(5000);
				quotaIntervalSearchParameter.setMax(10000);
			}

			if (3 == this.getValueRule().getQuotaIntervalType()) {
				quotaIntervalSearchParameter.setMax(10000);
			}

			parameter.setQuotaIntervalSearchParameter(quotaIntervalSearchParameter);
		}

		return (JSONObject) JSON.toJSON(parameter);
	}


	@Data
	public static class SearchParameter {

		private String key;

		private LatestCreateTimeSearchParameter latestCreateTimeSearchParameter;

		private QuotaIntervalSearchParameter quotaIntervalSearchParameter;

		@Data
		public static class LatestCreateTimeSearchParameter {

			private Integer createTime;
		}

		@Data
		public static class QuotaIntervalSearchParameter {

			private Integer min;

			private Integer max;
		}
	}

}

